/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.answer.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ruoyi.answer.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.answer.common.annotation.Login;
import com.ruoyi.answer.entity.AdminUser;
import com.ruoyi.answer.entity.LoginLog;
import com.ruoyi.answer.entity.User;
import com.ruoyi.answer.service.AdminUserService;
import com.ruoyi.answer.service.LoginLogService;
import com.ruoyi.answer.service.UserService;
import com.ruoyi.answer.tool.R;
import com.ruoyi.common.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * 登录接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "登录接口")
@AllArgsConstructor
public class ApiLoginController {

    public AdminUserService adminUserService;
    public UserService userService;
    public LoginLogService loginLogService;

    @PostMapping("admin/login")
    @ApiOperation("登录")
    public R adminLogin(@RequestBody AdminUser User) {
        if (StrUtil.isNotBlank(User.getAccount())) {
            AdminUser login = adminUserService.login(User.getAccount(), User.getPassword());

            if (login != null) {
                String token = JwtUtil.sign(login.getAccount());
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("token",token);
                hashMap.put("balance",login.getBalance());
                return R.ok(hashMap);
            }
        }
        return R.error("用户名或者密码错误");
    }
    @GetMapping("admin/info")
    @ApiOperation("登录")
    @Login
    public R adminInfo(@RequestAttribute  String account) {
        BigDecimal balance = adminUserService.lambdaQuery().eq(AdminUser::getAccount, account).one().getBalance();
        R r = new R();
        r.put("msg", "添加成功");
        r.put("balance", balance);
        return r;
    }
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestBody User user) {


        User one = userService.lambdaQuery().eq(User::getAccount, user.getAccount()).last("limit 1").one();

        if (one == null) {
            user.setId(IdWorker.getId());
            user.setCreateTime(new Date());
            user.setNumber(0);
            user.setStatus("0");
            userService.save(user);
        } else {
            one.setUserId(one.getId());
            one.setCreateTime(user.getCreateTime());
            one.setValue(user.getValue());
            one.setIdentityNumber(user.getIdentityNumber());
            one.setName(user.getName());
            userService.saveOrUpdate(one);
        }

        LoginLog loginLogEntity = new LoginLog();

        BeanUtil.copyProperties(one, loginLogEntity);
        loginLogEntity.setCreateTime(new Date());
        loginLogEntity.setId(IdWorker.getId());
        loginLogService.save(loginLogEntity);
        //用户登录
        // Map<String, Object> map = userService.login(form);
        return R.ok();
    }

    @Login
    @PostMapping("logout")
    @ApiOperation("退出")
    public R logout(@ApiIgnore @RequestAttribute("userId") long userId) {
        //tokenService.deleteToken(String.valueOf(userId));
        return R.ok();
    }

}
